package com.cardone.platform.authority.controller;

import java.util.List;
import java.util.concurrent.Callable;

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cardone.common.dto.PaginationDto;
import com.cardone.common.dto.ReturnDataDto;
import com.cardone.common.util.ReturnDataUtils;
import com.cardone.context.ContextHolder;
import com.cardone.platform.authority.dto.UserGroupDto;
import com.cardone.platform.authority.service.UserGroupService;

/**
 * 用户组
 *
 * @author yaohaitao
 */
@Deprecated
@Setter
@Slf4j
@RequestMapping("/authority/userGroup/")
@Controller
public class UserGroupController {
	/**
	 * 初始化数据绑定
	 *
	 * @param webDataBinder
	 *          数据绑定
	 */
	// @InitBinder
	// @Deprecated
	// public void initBinder(final WebDataBinder webDataBinder) {
	// }

	/**
	 * 用户组：删除
	 *
	 * @param deleteUserGroup
	 *          用户组对象
	 * @return 处理结果，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "delete.json" }, method = RequestMethod.POST)
	@ResponseBody
	public Callable<ReturnDataDto<String>> deleteJson(final UserGroupDto deleteUserGroup) {
		return new Callable<ReturnDataDto<String>>() {
			@Override
			public ReturnDataDto<String> call() throws Exception {
				try {
					ContextHolder.getBean(UserGroupService.class).deleteByIds(deleteUserGroup);

					return ReturnDataUtils.newReturnData();
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, UserGroupController.log);
				}
			}
		};
	}

	/**
	 * 用户组：分页查询
	 *
	 * @param findUserGroup
	 *          用户组对象
	 * @param model
	 *          上下文映射
	 */
	@Deprecated
	@RequestMapping(value = { "edit.html" })
	public void editHtml(final UserGroupDto findUserGroup, final Model model) {
		final UserGroupDto userGroup = ContextHolder.getBean(UserGroupService.class).findById(UserGroupDto.class, findUserGroup);

		model.addAttribute("userGroup", userGroup);
	}

	/**
	 * 用户组：编辑
	 *
	 * @param findUserGroup
	 *          用户组对象
	 * @return 实体对象集，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "findById.json" })
	@ResponseBody
	public Callable<ReturnDataDto<UserGroupDto>> findByIdJson(final UserGroupDto findUserGroup, final Model model) {
		return new Callable<ReturnDataDto<UserGroupDto>>() {
			@Override
			public ReturnDataDto<UserGroupDto> call() throws Exception {
				try {
					final UserGroupDto userGroup = ContextHolder.getBean(UserGroupService.class).findById(UserGroupDto.class, findUserGroup);

					return ReturnDataUtils.newReturnData(userGroup);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, UserGroupController.log);
				}
			}
		};
	}

	/**
	 * 用户组：查询
	 *
	 * @param findListUserGroup
	 *          用户组对象
	 * @return 用户组对象集合，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "findListByLikeCode.json" })
	@ResponseBody
	public Callable<ReturnDataDto<List<UserGroupDto>>> findListByLikeCodeJson(final UserGroupDto findListUserGroup) {
		return new Callable<ReturnDataDto<List<UserGroupDto>>>() {
			@Override
			public ReturnDataDto<List<UserGroupDto>> call() throws Exception {
				try {
					final List<UserGroupDto> userGroupList = ContextHolder.getBean(UserGroupService.class).findListByLikeCode(UserGroupDto.class, findListUserGroup);

					return ReturnDataUtils.newReturnData(userGroupList);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, UserGroupController.log);
				}
			}
		};
	}

	/**
	 * 用户组：分页查询
	 *
	 * @param paginationUserGroup
	 *          用户组对象
	 * @return 分页，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "paginationByLikeCode.json" })
	@ResponseBody
	public Callable<ReturnDataDto<PaginationDto<UserGroupDto>>> paginationByLikeCodeJson(final UserGroupDto paginationUserGroup) {
		return new Callable<ReturnDataDto<PaginationDto<UserGroupDto>>>() {
			@Override
			public ReturnDataDto<PaginationDto<UserGroupDto>> call() throws Exception {
				try {
					final PaginationDto<UserGroupDto> userGroupPagination = ContextHolder.getBean(UserGroupService.class).paginationByLikeCode(UserGroupDto.class, paginationUserGroup);

					return ReturnDataUtils.newReturnData(userGroupPagination);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, UserGroupController.log);
				}
			}
		};
	}

	/**
	 * 用户组：分页查询
	 *
	 * @param paginationUserGroup
	 *          用户组对象
	 * @param model
	 *          上下文映射
	 */
	@Deprecated
	@RequestMapping(value = { "pagination.html" })
	public void paginationHtml(final UserGroupDto paginationUserGroup, final Model model) {
		final PaginationDto<UserGroupDto> userGroupPagination = ContextHolder.getBean(UserGroupService.class).paginationByLikeCode(UserGroupDto.class, paginationUserGroup);

		model.addAttribute("userGroupPagination", userGroupPagination);
	}

	/**
	 * 用户组：保存
	 *
	 * @param saveUserGroup
	 *          用户组对象
	 * @return 处理结果，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "save.json" })
	@ResponseBody
	public Callable<ReturnDataDto<String>> saveJson(final UserGroupDto saveUserGroup) {
		return new Callable<ReturnDataDto<String>>() {
			@Override
			public ReturnDataDto<String> call() throws Exception {
				try {
					ContextHolder.getBean(UserGroupService.class).saveByIdOrCode(UserGroupDto.class, saveUserGroup);

					return ReturnDataUtils.newReturnData();
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, UserGroupController.log);
				}
			}
		};
	}
}
